//---------------------------------------------\\
// Personal Chest Plugin for CraftBukkit Servers \\
//---------------2014 Ryan Harrison----------------\\
package uk.co.vertigomoon.pchest;

import java.util.HashMap;
import java.util.Map;

import org.bukkit.entity.Player;

/**
 * Method to handle the state of personal chest creation for {@link Player}s
 */
public class PlayerManager {
   /**enum defining the states each player can be in**/
   public enum ChestState {
      LOCK,
      UNLOCK,
      NONE
   }//End enum ChestState

   /**Constant defining the message to show a player when they enter the locked state**/
   public static final String LOCKED_MESSAGE = "Any chest you create from now will be locked to your access only";
   /**Constant defining the message to show a player when they enter the unlocked state**/
   public static final String UNLOCKED_MESSAGE = "Any locked chest you own that you access from now on will be unlocked to public access";
   /**Constant defining the message to show a player when they enter the none state**/
   public static final String NONE_MESSAGE = "Interacting with chests will do nothing from now on";

   /**{@link Map} of {@link Player}s with thier current {@link ChestState} as the value*/
   private Map<Player, ChestState> playerMap;

   /**
    * Constructs a new {@link PlayerManager}
    */
   public PlayerManager(){
      playerMap = new HashMap<Player, ChestState>();
   }//End constructor

   /**
    * Updates the specified {@link Player} with the given {@link ChestState}.
    * @param player the {@link Player} to update.
    * @param state the {@link ChestState} to set for the player.
    * @param sendMessage if true, sends the player an update message describing the new state.
    */
   public void updateState(Player player, ChestState state, boolean sendMessage){
      playerMap.put(player, state);
      if(sendMessage){
         switch(state){
         case LOCK:
            player.sendMessage(LOCKED_MESSAGE);
            break;
         case NONE:
            player.sendMessage(NONE_MESSAGE);
            break;
         case UNLOCK:
            player.sendMessage(UNLOCKED_MESSAGE);
            break;
         default:
            break;
         }//End switch
      }//End if
   }//End method updateState

   /**
    * Returns the {@link ChestState} of the given {@link Player}.
    * @param player the {@link Player} to get the {@link ChestState} for.
    * @return the {@link ChestState} of the specified {@link Player}.
    */
   public ChestState checkState(Player player){
      if(playerMap.containsKey(player)){
         return playerMap.get(player);
      } else {
         return ChestState.NONE;
      }//End if
   }//End method checkState

   /**
    * Removes the given {@link Player} from this manager.
    * @param player the player to remove.
    */
   public void disconnectPlayer(Player player){
      if(playerMap.containsKey(player)){
         playerMap.remove(player);
      }//End if
   }//End method disconnectPlayer

}//End class PlayerManager
